package gam.t1.appraisal2.dinv.impl;

import gam.t1.appraisal2.Data;
import gam.t1.appraisal2.dinv.AppraisalMsg;
import gam.t1.appraisal2.dinv.DataInventory;
import gam.t1.appraisal2.dinv.sys.CurrentSystem;
import gam.t1.appraisal2.dinv.sys.PageSystem;
import gam.t1.appraisal2.dinv.tool.RecordSheet;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import pers.neige.neigeitems.action.ActionContext;
import pers.neige.neigeitems.item.ItemInfo;
import pers.neige.neigeitems.item.action.ActionTrigger;
import pers.neige.neigeitems.item.action.ItemAction;
import pers.neige.neigeitems.manager.ActionManager;
import pers.neige.neigeitems.utils.ItemUtils;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public final class AppraisalDataInventory extends DataInventory {
    private final List<String> NI_ITEMS ;
    private final List<String> NI_APPLICATIONS ;

    private PageSystem<AppraisalDataInventory> Actions;
    private PageSystem<AppraisalDataInventory> Appraisal;
    private CurrentSystem<AppraisalDataInventory> Corrects;

    public AppraisalDataInventory(String inventoryName, List<String> NI_ITEMS, List<String> NI_APPLICATIONS) {
        super(inventoryName);
        this.NI_ITEMS = NI_ITEMS;
        this.NI_APPLICATIONS = NI_APPLICATIONS;
        initialize();
    }
    private void initialize(){
        Corrects = new CurrentSystem<>(this,cS -> {
            AppraisalDataInventory handle = (AppraisalDataInventory) cS.getHandle();
            List<String> var1 = handle.getInventoryNitems();
            List<String> var2 = handle.getInventoryNiAppraisal();
            Player clickP = Data.getPlayerByDataInventory(handle);
            if (clickP == null) return false;
            boolean doNext;
            RecordSheet var = new RecordSheet();
            ItemStack[] pK = clickP.getInventory().getContents();
            if (var2.size() > 0){
                for (String _ni : var2){
                    ItemStack NeigeItem = null;
                    boolean _h = false;
                    int am = 1;
                    if (_ni.contains("|")){
                        String[] _vs = _ni.split("\\|");
                        try {am = Integer.parseInt(_vs[1]);} catch (NumberFormatException err) {err.printStackTrace();}
                        _ni = _vs[0];
                    }
                    if (Data.NI_ITEMMANAGER.hasItem(_ni)) {
                        NeigeItem = Data.NI_ITEMMANAGER.getItemStack(_ni);
                        NeigeItem.setAmount(am);
                    }
                    for (int i = 0; i < 36; i++) {
                        if (pK[i] != null){
                            ItemInfo niItem = ItemUtils.isNiItem(pK[i]);
                            ItemInfo niItem1 = ItemUtils.isNiItem(NeigeItem);
                            if (niItem1.getId().equals(niItem.getId())){
                                int need = NeigeItem.getAmount();
                                var.record(pK[i], need, pK[i].getAmount(),i);
                                _h = true;
                            }
                        }
                    }
                    if (!_h && NeigeItem != null) {
                        clickP.closeInventory();
                        clickP.sendMessage(AppraisalMsg.AP_FAIL);
                        return false;
                    }
                }
            }
            doNext = var.settlement(clickP);
            if (!doNext){
                clickP.closeInventory();
                clickP.sendMessage(AppraisalMsg.AP_FAIL);
                return false;
            }
            ActionContext aC = new ActionContext(clickP);
            ConcurrentHashMap<String, ItemAction> _tempA = ActionManager.INSTANCE.getItemActions();
            HashMap<String, ActionTrigger> _tempB;
            for (String item : var1) {
                for (String NitemName : _tempA.keySet()) {
                    _tempB = _tempA.get(NitemName).getTriggers();
                    for (String actionN : _tempB.keySet()) {
                        if (item.equals(NitemName)){
                            ActionTrigger tr = _tempB.get(actionN);
                            tr.run(aC);
                        }
                    }
                }
            }
            clickP.closeInventory();
            clickP.sendMessage(AppraisalMsg.AP_SUCCESS);
            return false;
        },cS -> {
            AppraisalDataInventory handle = (AppraisalDataInventory) cS.getHandle();
            Player clickP = Data.getPlayerByDataInventory(handle);
            if (clickP == null) return false;
            clickP.closeInventory();
            return false;
        });
        Actions = new PageSystem<>(this, 1, 1,NI_ITEMS.size(), null, null);
        PageSystem.PageView<AppraisalDataInventory> v1 = new PageSystem.PageView<>(Actions, new int[]{10,11, 19, 20, 28, 29}, pageS -> {
            List<String> var1 = pageS.getHandle().getInventoryNitems();
            if (var1.size() > pageS.getView().a().length)
                var1 = var1.subList((pageS.currentPageSize() - 1) * pageS.getView().a().length, Math.min(pageS.currentPageSize() * pageS.getView().a().length, var1.size()));
            if (pageS.getView().b() != -1 && pageS.getView().b() < var1.size()) {
                String v = var1.get(pageS.getView().b());
                pageS.getView().c();
                if (Data.NI_ITEMMANAGER.hasItem(v)){
                    return Data.NI_ITEMMANAGER.getItemStack(v);
                }
            }
            return Data.EMPTY;
        });
        PageSystem.PageController<AppraisalDataInventory> v2 = new PageSystem.PageController<>(Actions, 37, 38);
        Actions.setView(v1);
        Actions.setController(v2);
        Appraisal = new PageSystem<>(this, 1, 1,NI_APPLICATIONS.size(), null, null);
        PageSystem.PageView<AppraisalDataInventory> v3 = new PageSystem.PageView<>(Appraisal, new int[]{13,14,22,23,31,32},pageS -> {
            List<String> var1 = pageS.getHandle().getInventoryNiAppraisal();
            if (var1.size() > pageS.getView().a().length)
                var1 = var1.subList((pageS.currentPageSize() - 1) * pageS.getView().a().length, Math.min(pageS.currentPageSize() * pageS.getView().a().length, var1.size()));
            if (pageS.getView().b() != -1 && pageS.getView().b() < var1.size()) {
                String v = var1.get(pageS.getView().b());
                pageS.getView().c();
                int am = 1;
                if (v.contains("|")){
                    String[] _vs = v.split("\\|");
                    try {am = Integer.parseInt(_vs[1]);} catch (NumberFormatException err) {err.printStackTrace();}
                    v = _vs[0];
                }
                if (Data.NI_ITEMMANAGER.hasItem(v)) {
                    ItemStack NI_ITEM = Data.NI_ITEMMANAGER.getItemStack(v);
                    NI_ITEM.setAmount(am);
                    return NI_ITEM;
                }
            }
            return Data.EMPTY;
        });
        PageSystem.PageController<AppraisalDataInventory> v4 = new PageSystem.PageController<>(Appraisal, 40, 41);
        Appraisal.setView(v3);
        Appraisal.setController(v4);
        add(Actions);
        add(Appraisal);
        add(Corrects);
    }
    public List<String> getInventoryNitems() {return NI_ITEMS;}
    public List<String> getInventoryNiAppraisal() {return NI_APPLICATIONS;}
    @Override
    public DataInventory clone() {return new AppraisalDataInventory(InventoryName,NI_ITEMS,NI_APPLICATIONS);}
}
